/*
 * @Author: 0x9DEFA478
 * @Date: 2024-02-03 16:12:25
 * @LastEditTime: 2024-02-24 00:04:45
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "H_Thread_std.hpp"
#include <thread>
#include "H_Concurrency_std.hpp"




namespace H{

  struct Thread_LL_std_Handle{
    Thread_LL_std* Handle;
    std::thread* thread;
    H::Concurrency::Lock_std lock;
    H::Concurrency::Signal_std stopSignal;
    H::Concurrency::Signal_std detachSignal;
  };

  Thread_LL_std::Thread_LL_std(DelegateInvokeAbstract* invoke):Thread_LL_Abstract(invoke){
    Handle=static_cast<void*>(new Thread_LL_std_Handle);
    static_cast<Thread_LL_std_Handle*>(Handle)->Handle=this;
    static_cast<Thread_LL_std_Handle*>(Handle)->thread=nullptr;
  }

  Thread_LL_std::~Thread_LL_std(){
    std::thread* thread;

    thread=static_cast<Thread_LL_std_Handle*>(Handle)->thread;
    if(thread!=nullptr){
      delete thread;
    }
    delete static_cast<Thread_LL_std_Handle*>(Handle);
  }

  void Thread_LL_std::Start(){
    if(static_cast<Thread_LL_std_Handle*>(Handle)->thread==nullptr){
      static_cast<Thread_LL_std_Handle*>(Handle)->thread=new std::thread([this](){
        Thread_LL_std_Handle* Handle=static_cast<Thread_LL_std_Handle*>(this->Handle);
        bool IsDelete;

        (*invoke)();
        
        IsDelete=false;
        Handle->lock.lock();
        if(Handle->detachSignal.wait_ms(0)){
          IsDelete=true;
        }
        Handle->stopSignal.set();
        Handle->lock.unlock();
        if(IsDelete){
          auto invoke=this->invoke;
          delete this;
          delete invoke;
        }
      });
    }
  }

  void Thread_LL_std::Start(size StackSize,size Priority,const char* ThreadName){
    Start();
  }

  void Thread_LL_std::Join(){
    std::thread* thread;

    thread=static_cast<Thread_LL_std_Handle*>(Handle)->thread;
    if(thread!=nullptr){
      thread->join();
      delete thread;
      static_cast<Thread_LL_std_Handle*>(Handle)->thread=nullptr;
    }

  }

  bool Thread_LL_std::Detach(){
    Thread_LL_std_Handle* Handle=static_cast<Thread_LL_std_Handle*>(this->Handle);
    bool r;
    
    r=false;

    Handle->lock.lock();
    if(!Handle->stopSignal.wait_ms(0)){
      Handle->thread->detach();
      Handle->detachSignal.set();
      r=true;
    }
    Handle->lock.unlock();

    return r;
  }

}



